package com.css.fxfzaqbz.modules.assess2.repository.impl;

import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.assess2.entity.KeyplaceRiskassessment;
import com.css.fxfzaqbz.modules.assess2.entity.TaskContent;
import com.css.fxfzaqbz.modules.assess2.repository.KeyplaceRiskassessmentRepository;
import com.css.fxfzaqbz.modules.data.entity.AidedAnalyse;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;

@Repository
public class KeyplaceRiskassessmentRepositoryImpl implements KeyplaceRiskassessmentRepository {

    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public KeyplaceRiskassessment savekeyplaceRiskassessment(KeyplaceRiskassessment keyplaceRiskassessment) {
        String sql = "insert into sys_keyplace_riskassessment(kr_id,kr_content,kr_date,kr_people," +
                "del_flag,create_time,create_user,kr_relation,types,picture_id,title,description,status,data_message,zdmb) values (:krId,:krContent,:krDate," +
                ":krPeople,:delFlag,:createTime,:createUser,:krRelation,:types,:pictureId,:title,:description,:status,:dataMessage,:zdmb)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(keyplaceRiskassessment);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
        return keyplaceRiskassessment;
    }

    @Override
    public void updateKeyplaceRiskassessment(KeyplaceRiskassessment keyplaceRiskassessment) {
        String sql = "update sys_keyplace_riskassessment set picture_id=:pictureId ,title=:title,description=:description,update_user=:updateUser," +
                "update_time=:updateTime where kr_id='" + keyplaceRiskassessment.getKrId() + "'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(keyplaceRiskassessment);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
    }

    @Override
    public List<TaskContent> getKeyplaceRiskassessmentByRelation1(String id) {


        String sql = "select kr_id id, kr_content as content,kr_date as dateTime from sys_keyplace_riskassessment where kr_relation='" + id + "' and del_flag='" + YNEnum.N + "'";
        List<TaskContent> keyplaceRiskassessment = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(TaskContent.class));
        return keyplaceRiskassessment;

    }

    @Override
    public List<KeyplaceRiskassessment> getKeyplaceRiskassessmentByIaId(String activeId) {
        String sql = "select * from sys_keyplace_riskassessment where kr_relation='" + activeId + "' and del_flag='" + YNEnum.N + "' and status !='" + YNEnum.N + "'";
        List<KeyplaceRiskassessment> keyplaceRiskassessments = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(KeyplaceRiskassessment.class));
        return keyplaceRiskassessments;
    }

    @Override
    public List<KeyplaceRiskassessment> check(KeyplaceRiskassessment keyplaceRiskassessment) {
        String sql = "select * from sys_keyplace_riskassessment where kr_relation = '" + keyplaceRiskassessment.getKrRelation() + "' and del_flag = '"+YNEnum.N+"' and title = '"+keyplaceRiskassessment.getTitle()+"'";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(KeyplaceRiskassessment.class));
    }

    @Override
    public List<KeyplaceRiskassessment> getByEntity(KeyplaceRiskassessment keyplaceRiskassessment) {
        String sql = "select * from sys_keyplace_riskassessment where kr_relation = '" + keyplaceRiskassessment.getKrRelation() + "' and del_flag = '"+YNEnum.N+"' and kr_content = '"+keyplaceRiskassessment.getKrContent()+"' and status = '"+YNEnum.Y+"'";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(KeyplaceRiskassessment.class));
    }

    @Override
    public void submitResult(String id, String description) {
        String sql = "update sys_keyplace_riskassessment set" +
                " description = '"+description+"', " +
                " status = '"+YNEnum.Y+"' " +
                " where kr_id = '" + id + "'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void removeResult(String id, String description) {
        String sql = "update sys_keyplace_riskassessment set" +
                " description = '"+description+"', " +
                " submiter = '"+description+"', " +
                " submit_phone = '"+description+"', " +
                " status = '"+YNEnum.N+"' " +
                " where kr_id = '" + id + "'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void finalSubmission(List<String> ids, String userName, String mobilePhone,Timestamp currentTimestamp) {
        String sql = "update sys_keyplace_riskassessment set" +
                " submiter = '"+userName+"', " +
                " submit_phone = '"+mobilePhone+"', " +
                " update_time = '"+currentTimestamp+"', " +
                " submit_time = '"+currentTimestamp+"', " +
                " status = '"+2+"' " +
                " where kr_id in (:ids) ";
        HashMap<String, Object> map = new HashMap<>();
        map.put("ids",ids);
        namedParameterJdbcTemplate.update(sql,map);
    }

    @Override
    public void saveDescription(List<String> ids, String description) {
        String sql = "update sys_keyplace_riskassessment set" +
                " description = '"+description+"' " +
                " where kr_id in (:ids) ";
        HashMap<String, Object> map = new HashMap<>();
        map.put("ids",ids);
        namedParameterJdbcTemplate.update(sql,map);
    }

    @Override
    public List<KeyplaceRiskassessment> getZdByIaId(String id) {
        String sql = "select * from sys_keyplace_riskassessment where kr_relation='" + id + "' and del_flag='" + YNEnum.N + "' and status='" + 2 + "'";
        List<KeyplaceRiskassessment> keyplaceRiskassessments = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(KeyplaceRiskassessment.class));
        return keyplaceRiskassessments;
    }

    @Override
    public List<KeyplaceRiskassessment> getByIaIdAndContent(String iaId, String zdcsfxpg) {
        String sql = "select * from sys_keyplace_riskassessment where kr_relation='" + iaId + "' and del_flag='" + YNEnum.N + "' and  kr_content='"+zdcsfxpg+"' and status='" + YNEnum.Y + "'";
        List<KeyplaceRiskassessment> keyplaceRiskassessments = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(KeyplaceRiskassessment.class));
        return keyplaceRiskassessments;
    }

    //有用
    @Override
    public List<KeyplaceRiskassessment> getByIaId(String activeId) {
        String sql = "select * from sys_keyplace_riskassessment where kr_relation='" + activeId + "' and del_flag='" + YNEnum.N + "' and status='" + YNEnum.N + "' order by create_time desc";
        List<KeyplaceRiskassessment> keyplaceRiskassessments = null;
        try {
            keyplaceRiskassessments = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(KeyplaceRiskassessment.class));
            return keyplaceRiskassessments;
        } catch (DataAccessException e) {
            e.printStackTrace();
            return null;
        }

    }


    @Override
    public KeyplaceRiskassessment getKeyplaceRiskassessmentById(String id) {
        String sql = "select * from sys_keyplace_riskassessment where kr_id='" + id + "' and del_flag='" + YNEnum.N + "'";
        KeyplaceRiskassessment keyplaceRiskassessment = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(KeyplaceRiskassessment.class));
        return keyplaceRiskassessment;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteKeyplaceRiskassessment(String id, String userId, Timestamp currentTimestamp) {
        String sql = "update sys_keyplace_riskassessment set del_Flag='" + YNEnum.Y + "',update_time='" + currentTimestamp + "',update_user='" + userId + "' where kr_id ='" + id + "'";
        jdbcTemplate.update(sql);
    }
}
